﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace JSMixer
{
    public class FileAction
    {
        public FileAction()
        {
            Files = new List<CheckItem>();
        }
        public List<CheckItem> Files
        {
            get;
            set;
        }
    }

    public class Combiner : FileAction
    {
        public Combiner()
        { 
        }
        public Combiner(List<CheckItem> files)
        {
            this.Files = files;
        }

        public bool IncludeHeader
        {
            get;
            set;
        }

        public string GetCombination()
        {
            StringBuilder builder = new StringBuilder();

            foreach (CheckItem item in Files)
            {

                if (IncludeHeader)
                {
                    builder.Append("/*");
                    builder.Append('*', 78);
                    builder.AppendLine();
                    builder.AppendLine(item.FileName);
                    builder.Append('*', 78);
                    builder.Append("*/");
                    builder.AppendLine();
                }
                using (StreamReader streamReader = new StreamReader(item.FilePath))
                {
                    builder.AppendLine(streamReader.ReadToEnd());
                }
            }

            return builder.ToString();
        }
    }


    public class Compressor : FileAction
    {
        public string GetMergedAndCompressed()
        {
            Combiner cb = new Combiner(this.Files);
            return GetCompressed(cb.GetCombination());
        }

        public string GetCompressed(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }

            return Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(input);
        }
    }
}
